//
//  WDOriImageController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/28.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDOriImageController.h"
#import "WDOriImagecell.h"

static NSString *cellID = @"OriImageCell";

@interface WDOriImageController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *imageCollectionView;
/** 图片张数 */
@property (nonatomic, strong) NSString *imagePage;
/** 头部视图 */
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation WDOriImageController

#pragma mark - 懒加载
- (UICollectionView *)imageCollectionView
{
    if (!_imageCollectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        //水平方向滑动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        //行间距
        layout.minimumLineSpacing = 0;
        //列间距
        layout.minimumInteritemSpacing = 0;
        
        _imageCollectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
        
        //点击的是哪张图片，就跳转到哪
        _imageCollectionView.contentOffset = CGPointMake(WDScreenW *self.index, 0);
        
        //register
        [_imageCollectionView registerClass:[WDOriImageCell class] forCellWithReuseIdentifier:cellID];
        
        _imageCollectionView.pagingEnabled = YES;
        _imageCollectionView.showsVerticalScrollIndicator = NO;
        _imageCollectionView.showsHorizontalScrollIndicator = NO;
        _imageCollectionView.delegate = self;
        _imageCollectionView.dataSource = self;
    }
    return _imageCollectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadNavItemWithAction:@selector(backBtn) btnImage:@"icon_back" btnSelImage:@"icon_back" title:nil];
    
    [self.view addSubview:self.imageCollectionView];
    
    [self setUpHeadView];
    
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpHeadView
{
    
    self.imagePage = [NSString stringWithFormat:@"%zd/%zd", self.index + 1, self.images.count];
    
    UIView *headView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, 64)];
    
    [self.view addSubview:headView];
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [backBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    
    [headView addSubview:backBtn];
    
    [backBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.centerY.equalTo(headView.centerY);
        make.width.height.equalTo(64);
    }];
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    
    titleLabel.text = self.imagePage;
    
    titleLabel.font = [UIFont systemFontOfSize:20];
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    titleLabel.textColor = [UIColor whiteColor];
    
    [headView addSubview:titleLabel];
    
    self.titleLabel = titleLabel;
    
    [titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView.centerY);
        make.centerX.equalTo(headView.centerX);
    }];
}

#pragma mark - <UICollectionView数据源 代理>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    WDOriImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    cell.imageURL = self.images[indexPath.row];
    
    return cell;
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
//collectionCell的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize = CGSizeMake(WDScreenW, WDScreenH);
    
    return cellSize;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    NSInteger index = scrollView.contentOffset.x / WDScreenW;
    
    self.titleLabel.text = [NSString stringWithFormat:@"%zd/%zd", index + 1, self.images.count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
